package cn.wgx.modules.sys.web;

import cn.wgx.commons.annotation.ApiPermission;
import cn.wgx.commons.util.StringUtils;
import cn.wgx.commons.util.Validation;
import cn.wgx.modules.base.web.BaseController;
import cn.wgx.modules.sys.entity.SysMsg;
import cn.wgx.modules.sys.entity.SysUser;
import cn.wgx.modules.sys.services.SysMsgService;
import cn.wgx.modules.sys.util.SysMsgUtil;
import cn.wgx.modules.sys.util.UserTypeUtil;
import cn.wgx.modules.sys.util.UserUtil;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Api(tags = "5. 消息管理")
@RestController
@RequestMapping(value = "${allPathPre}sysMsg")
public class SysMsgController extends BaseController {

    @Resource
    private SysMsgService sysMsgService;


    @ApiOperation(value = "获取消息列表(未读、时间倒序排列)", notes = "消息类型type：0系统消息,1通知消息;消息状态status：0未读 1已读;消息内容content：支持模糊查询；消息标题：支持模糊查询")
    @RequestMapping(path = "list", method = RequestMethod.GET)
    @ApiPermission
    public Map<String, Object> getList(@ModelAttribute SysMsg sysMsg,
                                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        sysMsg.setIsSend(false);
        sysMsg.setToUserId(UserUtil.getUser().getUser_id());
        return returnData(PageHelper.startPage(page,  Math.min(Math.abs(pageSize), maxPageSize)), sysMsgService.wrap(sysMsgService.getList(sysMsg, true)));
    }

    @ApiOperation(value = "获取我发布过的", notes = "消息类型type：0系统消息,1通知消息;")
    @RequestMapping(path = "listMyPublish", method = RequestMethod.GET)
    @ApiPermission
    public Map<String, Object> getListPublish(@ModelAttribute SysMsg sysMsg,
                                       @RequestParam(value = "page", defaultValue = "1") Integer page,
                                       @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize) {
        sysMsg.setIsSend(true);
        if(!UserTypeUtil.isAdmin()){
            sysMsg.setFromUserId(UserUtil.getUser().getUser_id());
        }
        return returnData(PageHelper.startPage(page, pageSize), sysMsgService.getList(sysMsg, true));
    }

    @ApiOperation("获取单个消息")
    @ApiPermission
    @RequestMapping(path = "get/{msg_id}", method = RequestMethod.GET)
    public Map<String, Object> getOne(@PathVariable Integer msg_id) {
        return returnData(sysMsgService.getOne(msg_id));
    }

    @ApiOperation("设置为已读")
    @ApiPermission
    @RequestMapping(path = "updateStatus/{msg_id}", method = RequestMethod.PATCH)
    public Map<String, Object> updateStatus(@PathVariable Integer msg_id) {
        return returnData(sysMsgService.updateStatus(msg_id));
    }


    @ApiOperation(value = "逻辑删除消息（单个、批量）" ,notes = "只能逻辑删除本人消息")
    @ApiPermission
    @RequestMapping(path = "del", method = RequestMethod.DELETE)
    public Map<String, Object> deleteBatch(@RequestBody List<Integer> msg_ids) {
        return returnData(sysMsgService.deleteBatch(msg_ids, true));
    }

    @ApiOperation("撤回群发消息")
    @ApiPermission
    @RequestMapping(path = "delPublish/{msg_id}", method = RequestMethod.DELETE)
    public Map<String, Object> review(@PathVariable Integer msg_id) {
        SysMsg one = sysMsgService.getOne(msg_id);
        if(one != null ){
            SysUser user = UserUtil.getUser();
            if(one.getIsSend() && user.getUser_id().equals(one.getFromUserId())){
                return returnData(sysMsgService.delPublish(one.getHashCode(), user.getUser_id()));
            }
        }
        return returnErrorMap("ID错误");
    }

//    @ApiOperation("彻底删除消息（单个、批量）")
//    @ApiPermission
//    @RequestMapping(path = "del", method = RequestMethod.DELETE)
//    public Map<String, Object> delete(@RequestParam List<Integer> msg_ids) {
//        return returnData(sysMsgService.deleteBatch(msg_ids, false));
//    }

    @ApiOperation(value = "群发短消息", notes = "rabbitmq实现; 必须参数: title, content, " +
            "(schoolCode, squadCode, toUserId 这三个参数至少有一个,按优先级判断群发),")
    @ApiPermission
    @RequestMapping(path = "add", method = RequestMethod.POST)
    public Map<String, Object> insertUser(@RequestBody SysMsg sysMsg) {
        if(sysMsg == null || StringUtils.isBlank(sysMsg.getTitle()) || StringUtils.isBlank(sysMsg.getContent())
            || (StringUtils.isBlank(sysMsg.getSchoolCode()) && StringUtils.isBlank(sysMsg.getSquadCode()) && !Validation.isNotNullOrZero(sysMsg.getToUserId()))){
            return returnErrorMap("参数错误");
        }
        SysUser user = UserUtil.getUser();
        if(!UserTypeUtil.isAdmin()){
        }
        sysMsg.setFromUserId(user.getUser_id());
        sysMsg.setIsSend(true);//发送历史记录
        sysMsg.setType(1);
        sysMsgService.insertOne(sysMsg);
        if(Validation.isNotNullOrZero(sysMsg.getMsg_id())){

        }
        return returnData(sysMsg);
    }
}
